package com.hsylient.javase.chapter05;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;

public class D_MyFileOutputStream {

    /**
     * FileOutputStream指定文件输出的位置，这里直接写的相对路径。idea默认位置为项目的根路径
     * boolean参数；是否追加输出，默认是覆盖，就是新的新的一次输出会覆盖之前输出的内容
     */
    @Test
    public void testWrite() {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("hello.txt", Boolean.TRUE);
            fileOutputStream.write(97);
            fileOutputStream.write(new byte[]{98, 99, 100, 101, 102});
            fileOutputStream.write("hsylient".getBytes());
            fileOutputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
